﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZFine.Code
{
    public static class EnumHelper
    {
        /**/
        /// <summary>
        /// 从Enum中任意取一个Int值，将其转化成枚举类型值
        /// </summary>
        /// <param name="protocolType"></param>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        /// <example>ExampleNormalEnum status = (ExampleNormalEnum)EnumHelper.IntValueToEnum( typeof( ExampleNormalEnum ),1); 得到值为 ExampleNormalEnum.Online </example>
        public static object IntValueToEnum(System.Type protocolType, int enumIntValue)
        {
            object myObject = Enum.Parse(protocolType, Enum.GetName(protocolType, enumIntValue));
            return myObject;
        }
        /**/
        /// <summary>
        /// 从Enum中任意取一个String值，将其转化成枚举类型值
        /// </summary>
        /// <param name="protocolType"></param>
        /// <param name="enumStringValue"></param>
        /// <returns></returns>
        /// <example>ExampleNormalEnum status = (ExampleNormalEnum)EnumHelper.StringValueToEnum( typeof( ExampleNormalEnum ),"Offline");得到值为 ExampleNormalEnum.Offline</example>
        public static object StringValueToEnum(System.Type protocolType, string enumStringValue)
        {
            object myObject = Enum.Parse(protocolType, enumStringValue, true);
            return myObject;
        }

        /**/
        /// <summary>
        /// 得到一个Enum中的所有Int值
        /// </summary>
        /// <param name="protocolType"></param>
        /// <returns></returns>
        public static int[] GetEnumIntValues(System.Type protocolType)
        {
            int[] myIntArray = new int[Enum.GetValues(protocolType).Length];
            Array.Copy(Enum.GetValues(protocolType), myIntArray, Enum.GetValues(protocolType).Length);
            return myIntArray;
        }

        /// <summary>
        /// 得到一个Enum中的所有string值
        /// </summary>
        public static Array GetEnumStringValues(System.Type protocolType)
        {
            return Enum.GetValues(protocolType);
        }

        /// <summary>
        /// 得到一个Enum中的所有string值
        /// </summary>
        public static List<string> GetEnumListStringValues(System.Type protocolType)
        {
            Array ar = Enum.GetValues(protocolType);
            ar.GetEnumerator();
            List<string> l = new List<string>();
            for (int i = 0; i < ar.Length; i++)
            {
                l.Add(ar.GetValue(i).ToString());
            }
            return l;
        }
    }
}
